package com.water.dynamic;

import com.water.aop.Aop;
import com.water.service.IService;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;

/**
 * @author wsj
 * @version 1.0
 * @description: TODO
 * @date 2022/1/9 23:22
 */
public class ProxyHandler implements InvocationHandler {
    private IService service;
    private Aop aop;

    public ProxyHandler(IService service, Aop aop) {
        this.service = service;
        this.aop = aop;
    }

    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        try{
            aop.before();
            Object invoke = method.invoke(service,args);
            aop.after();
            return invoke;
        }catch (Exception e){
            aop.exception();
            e.printStackTrace();
            throw e;
        }finally {
            aop.myFinally();

        }
    }
}
